package com.bot.backend.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Player {
    private Integer id;
    private Integer snakeId; // -1表示亲自上阵，否则表示用AI打
    private String snakeCode;
    private Integer sx; // 起点坐标
    private Integer sy;
    private List<Integer> steps; // 玩家每一步的操作

    // 蛇在什么时候变长
    private boolean check_tail_increasing(Integer step) {
        if (step <= 10) return true;
        if (step % 3 == 1) return true;
        return false;
    }

    // 返回蛇的身体
    public List<Cell> getCells() {
        List<Cell> res = new ArrayList<>();
        int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
        Integer x = sx, y = sy;
        res.add(new Cell(x, y));

        Integer step = 0;
        for (int i = 0; i < this.steps.size(); i ++ ) {
            Integer d = this.steps.get(i);
            x += dx[d];
            y += dy[d];
            res.add(new Cell(x, y));
            // 若蛇的长度不增加，删掉蛇尾
            if (!this.check_tail_increasing( ++ step)) {
                res.remove(0);
            }
        }

        return res;
    }

    public String getStringByListSteps() {
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer step : this.steps) {
            stringBuilder.append(step);
        }

        return stringBuilder.toString();
    }
}
